﻿#ifndef AutoFocusThread_H
#define AutoFocusThread_H

/**
 自动对焦
*/

#include <QtCore/QThread>
#include <QtCore/QList>
#include <QtCore/QVector>
#include <QtCore/QPoint>
#include <QtCore/QSemaphore>

#include "FocusMeasure.h"
#include "CameraCommon/CameraImage_RGB24.h"

class MainWindow;
class AutoFocusThread : public QThread
{
    Q_OBJECT
public:
    enum AUTO{ AUTO_IDLE, AUTO_LIST, AUTO_STEP };
    AutoFocusThread(QObject *parent = Q_NULLPTR);
    ~AutoFocusThread();
    void setMainWindow(MainWindow * win);
    void setCameraImage(CameraImage * cameraImage);
    void setROI(QRect ROI);
    void setZPositions(QList<double> zList);
    void setZStep(double zStep);
signals:
    void zMoveToPos(double z_mm, double speed);
    void zMoveToRelativePos(double z_mm, double speed);
    void autoFocusFinish();
public slots:
    void stop();
    void zMoveFinished();

private:
    MainWindow *m_win;
    CameraImage * m_cameraImage;
    QRect m_ROI;
    QImage m_bestImage;
    QList<double> m_zList;
    double m_zStep;
    int m_countUp;
    int m_CountDown;
    bool m_stop;
    QSemaphore m_zSemaphore;
    FocusMeasure m_focusMeasure;
    AUTO m_auto;
    void initSemaphore();
    bool waitingForZStop();
    bool waitGotoPos();

    void autoFocus_list();
    void autoFocus_step();
protected:
    void run();
};

#endif // AutoFocusThread_H
